package Classe;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author diegob
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.lang.String;


public class BD {



    protected Connection conectar() throws ClassNotFoundException, SQLException {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521","gurgel","gurgel");
    }

    public ResultSet consultar(String sql) throws ClassNotFoundException, SQLException{
        Connection conn = conectar();
        Statement stm = conn.createStatement();
        ResultSet resultado = stm.executeQuery(sql);

        return resultado;
    }


    public void executar(String insercao, Object[] parametros, Object[] tipos) throws Exception {
       Connection conn = conectar();
        try{
                // armazena a conexão com o banco.
                

                PreparedStatement stm = conn.prepareStatement(insercao);
                
                for(int i=0; i<parametros.length; i++){

                    if(tipos[i] == String.class){
                        if(parametros[i] != null)
                            stm.setString(i+1, (String) parametros[i]);
                        else
                            stm.setNull(i+1, Types.VARCHAR);
                    }

                    else if(tipos[i] == Integer.class){
                        if(parametros[i] != null)
                            stm.setInt(i+1, (Integer) parametros[i]);
                        else
                            stm.setNull(i+1, Types.INTEGER);
                    }

                    else if(tipos[i] == Double.class){
                        if(parametros[i] != null)
                            stm.setDouble(i+1, (Double) parametros[i]);
                        else
                            stm.setNull(i+1, Types.DOUBLE);
                    }
                }

                stm.executeUpdate();

        }catch(Exception e){
                e.printStackTrace();
                throw e;
        }
       finally{
           conn.close();
       }
    }

public ResultSet consulta(String insercao, Object[] parametros, Object[] tipos) throws Exception {

     ResultSet result = null;

    Connection conn = conectar();
        try{
                // armazena a conexão com o banco.


                PreparedStatement stm = conn.prepareStatement(insercao);

                for(int i=0; i<parametros.length; i++){

                    if(tipos[i] == String.class){
                        if(parametros[i] != null)
                            stm.setString(i+1, (String) parametros[i]);
                        else
                            stm.setNull(i+1, Types.VARCHAR);
                    }

                    else if(tipos[i] == Integer.class){
                        if(parametros[i] != null)
                            stm.setInt(i+1, (Integer) parametros[i]);
                        else
                            stm.setNull(i+1, Types.INTEGER);
                    }

                    else if(tipos[i] == Double.class){
                        if(parametros[i] != null)
                            stm.setDouble(i+1, (Double) parametros[i]);
                        else
                            stm.setNull(i+1, Types.DOUBLE);
                    }
                }

                stm.executeQuery(insercao);

        }catch(Exception e){
                e.printStackTrace();
                throw e;
        }
       finally{
           conn.close();
       }


       return result;

}

    
}

